﻿using AJWPFAdmin.Core;
using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Components.Views;
using AJWPFAdmin.Core.Excel;
using AJWPFAdmin.Core.ExtensionMethods;
using AJWPFAdmin.Core.Models;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Core.Mvvm;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.EF;
using MaterialDesignExtensions.Controls;
using MaterialDesignThemes.Wpf;
using Microsoft.AspNetCore.Mvc;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using AJWPFAdmin.Core.Components.AJFilePicker.ViewModels;
using Prism.Mvvm;
using AJWPFAdmin.Services;
using DocumentFormat.OpenXml.EMMA;

namespace AJWPFAdmin.Modules.Common.ViewModels
{
    public class AuthorizeDialogViewModel : ViewModelBase, IDialogAware
    {

        public event Action<IDialogResult> RequestClose;

        public string Title => "输入激活码";

        private string _value;
        public string Value
        {
            get => _value;
            set
            {
                SetProperty(ref _value, value);
                Info = string.Empty;
            }
        }

        private bool _loading;
        public bool Loading
        {
            get => _loading;
            set => SetProperty(ref _loading, value);
        }

        private string _info;
        public string Info
        {
            get => _info;
            set => SetProperty(ref _info, value);
        }

        private string _md5;
        public string MD5
        {
            get => _md5;
            set => SetProperty(ref _md5, value);
        }

        private DelegateCommand _copyMD5Cmd;
        public DelegateCommand CopyMD5Cmd =>
            _copyMD5Cmd ?? (_copyMD5Cmd = new DelegateCommand(ExecuteCopyMD5Cmd));

        void ExecuteCopyMD5Cmd()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(MD5))
                {
                    Clipboard.SetText(MD5);
                    MessageBox.Show("复制成功", "提示", MessageBoxButton.OK);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "复制失败", MessageBoxButton.OK);
            }
        }

        private DelegateCommand<bool?> _confirmCmd;
        public DelegateCommand<bool?> ConfirmCmd =>
            _confirmCmd ?? (_confirmCmd = new DelegateCommand<bool?>(ExecuteConfirmCmd));

        async void ExecuteConfirmCmd(bool? parameter)
        {
            if (!parameter.GetValueOrDefault())
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
                return;
            }

            if (string.IsNullOrWhiteSpace(Value))
            {
                Info = "激活码必填";
                return;
            }

            Loading = true;

            var result = await _cfgSvc.TryActiveAsync(Value, false, false);
            if (!result.Success)
            {
                Info = result.Message;
                Loading = false;
                return;
            }

            Loading = false;
            Info = string.Empty;

            MessageBox.Show("感谢您的使用!", "激活成功", MessageBoxButton.OK);

            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }

        private AJConfigService _cfgSvc;

        public AuthorizeDialogViewModel(AJConfigService cfgSvc)
        {
            _cfgSvc = cfgSvc;
            MD5 = _cfgSvc.MACHINEMD5;
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }
    }
}
